﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using MyOS.Models;
using Panuon.UI.Silver;

namespace MyOS.Views.ChildView
{
    /// <summary>
    ///     ResultView.xaml 的交互逻辑
    /// </summary>
    public partial class ResultView : WindowX
    {
        public ResultView()
        {
            InitializeComponent();
        }

        private void WindowX_Loaded(object sender, RoutedEventArgs e)
        {
            //读入目录树
            var path = @"../data";
            ListView.Items.Clear();
            var files = Directory.GetFiles(path);
            var item = new ListViewItem();

            foreach (var file in files)
            {
                var processFileModel =
                    new ProcessFileModel(Path.GetFileName(file), Path.GetFullPath(file));
                ListView.Items.Add(processFileModel);
            }
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //查看内容
            if (ListView.SelectedItems.Count > 0)
            {
                var processFileModel = ListView.SelectedItem as ProcessFileModel;
                var path = processFileModel.Tag;
                using (var stream = new FileStream(path, FileMode.Open))
                {
                    var buffer = new byte[1024 * 10];
                    stream.Read(buffer, 0, buffer.Length);
                    TextEditor.Text = Encoding.Default.GetString(buffer);
                }
            }
        }
    }
}